package rest

import (
	"fmt"

	"github.com/labstack/echo/v4"
	"github.com/pkg/errors"

	"bilinadmin/pkg/lueerror"

	pb "bilinadmin/api/pb/server"
	"bilinadmin/internal/service"
)

type empHandler struct {
	*Base
	name string
}

// NewRestEmployeeHandler 员工 handler
func NewRestEmployeeHandler() *empHandler {
	return &empHandler{
		Base: base,
		name: "rest_employee_handler",
	}
}

// Register 路由注册
func (e *empHandler) Register(r *echo.Group) {
	r.POST("/v1/login", e.loginBilin)
	// 员工邀请
	r.POST("/v1/invitation", e.employeeInvitation)
	// 员工信息
	r.GET("/v1/info", e.getEmployeeInfo)
	// 更新员工信息
	r.PUT("/v1/info/update", e.updateEmpInfo)
	// 获取员工信息分享 token
	r.GET("/v1/share/token", e.getShareToken)
	// 根据 share token 获取员工信息
	r.GET("/v1/share/info", e.getEmpInfoByShare)
	// 运营员工列表
	r.GET("/v1/op/list", e.getEmpOpList)
	// 员工确认加入短信
	r.POST("/v1/confirm/code", e.sendConfirmCode)
	// 员工确认加入
	r.POST("/v1/invitation/confirm", e.empConfirmInvitation)
	// 运营离职
	r.PUT("/v1/op/depart", e.operatorDepart)
	// 运营重新入职
	r.PUT("/v1/op/re-enroll", e.operatorReEnroll)
	// 运营删除
	r.PUT("/v1/op/delete", e.operatorDelete)
	// 重置超管
	r.PUT("/v1/sup-admin/reset", e.resetSupAdmin)
	// 获取超级管理员
	r.GET("/v1/sup-admin", e.getSupAdmin)
	// 获取管理的品牌
	r.GET("/v1/related/brands", e.getRelatedBrands)
	// 获取门店或门店运营角色权限信息
	r.GET("/v1/op/info", e.getOperatorInfo)

}

// 员工登陆
func (e *empHandler) loginBilin(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.VerifyAccountToken(c)
	if errL != nil {
		return errL
	}
	req := &pb.EmployeeLoginReq{}
	if errL = e.ParseBody(c, req); errL != nil {
		return errL
	}
	ret, errL := service.GetEmployeeService().LoginBilin(ctx, req.BrandId, token.GID)
	if errL != nil {
		return errL
	}
	ret.Token, errL = service.GetService().GenBilinToken(token.GID, ret.EmpName, ret.BrandId, ret.EmpId)
	return e.SimpleRet(c, ret, errL)
}

//  员工邀请
func (e *empHandler) employeeInvitation(c echo.Context) error {
	ctx := c.Request().Context()
	// 权限校验
	token, errL := e.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	storeId, errL := parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	req := &pb.EmployeeInvitationReq{}
	if errL = e.ParseBody(c, req); errL != nil {
		return errL
	}
	req.StoreId = storeId
	ret, errL := service.GetEmployeeService().InviteEmployee(ctx, token.BrandId, req, token.EmpId)
	return e.SimpleRet(c, ret, errL)
}

// 获取员工信息
func (e *empHandler) getEmployeeInfo(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	storeId, errL := parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	empId, errL := parseEmpId(c, true)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetEmployeeService().GetEmployeeInfo(ctx, token.BrandId, storeId, empId)
	return e.SimpleRet(c, ret, errL)
}

func (e *empHandler) getShareToken(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	storeId, errL := parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	empId, errL := parseEmpId(c, true)
	if errL != nil {
		return errL
	}
	shareToken, errL := service.GetEmployeeService().GetEmpInfoShareToken(ctx, token.BrandId, storeId, empId)
	if errL != nil {
		return errL
	}
	ret := map[string]interface{}{
		"share_token": shareToken,
	}
	return e.SimpleRet(c, ret, nil)
}

func (e *empHandler) getEmpInfoByShare(c echo.Context) error {
	ctx := c.Request().Context()
	shareToken := c.QueryParam("share_token")
	if shareToken == "" {
		return lueerror.InvalidRequest(errors.New("参数 share_token 不为空"))
	}
	ret, errL := service.GetEmployeeService().GetEmpInfoByShareToken(ctx, shareToken)
	return e.SimpleRet(c, ret, errL)
}

func (e *empHandler) updateEmpInfo(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.EmployeeUpdateReq{}
	if errL = e.ParseBody(c, req); errL != nil {
		return errL
	}
	req.StoreId, errL = parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetEmployeeService().UpdateEmpInfo(ctx, token.BrandId, req, token.EmpId)
	return e.SimpleRet(c, ret, errL)
}

func (e *empHandler) getEmpOpList(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	storeId, errL := parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	pagination := paginationParse(c)
	status := c.QueryParam("status")
	search := c.QueryParam("search")
	ret, errL := service.GetEmployeeService().GetOperationList(ctx, token.BrandId, storeId, status, search, pagination)
	return e.SimpleRet(c, ret, errL)
}

func (e *empHandler) sendConfirmCode(c echo.Context) error {
	ctx := c.Request().Context()
	req := &pb.EmployeeSendConfirmCodeReq{}
	if errL := e.ParseBody(c, req); errL != nil {
		return errL
	}
	errL := service.GetEmployeeService().SendConfirmCode(ctx, req.ShareToken, req.CodeType)
	return e.SimpleRet(c, nil, errL)
}

// 接受邀请
func (e *empHandler) empConfirmInvitation(c echo.Context) error {
	ctx := c.Request().Context()
	req := &pb.EmployeeConfirmInvitationReq{}
	if errL := e.ParseBody(c, req); errL != nil {
		return errL
	}
	errL := service.GetEmployeeService().EmpConfirmInvitation(ctx, req)
	return e.SimpleRet(c, nil, errL)
}

// 运营离职
func (e *empHandler) operatorDepart(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.EmployeeDepartReq{}
	if errL = e.ParseBody(c, req); errL != nil {
		return errL
	}
	req.StoreId, errL = parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	errL = service.GetEmployeeService().OperatorDepart(ctx, token.BrandId, req, token.EmpId)
	return e.SimpleRet(c, nil, errL)
}

func (e *empHandler) operatorReEnroll(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.EmployeeReEnrollReq{}
	if errL = e.ParseBody(c, req); errL != nil {
		return errL
	}
	req.StoreId, errL = parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	errL = service.GetEmployeeService().OperatorReEnroll(ctx, token.BrandId, req, token.EmpId)
	return e.SimpleRet(c, nil, errL)
}

func (e *empHandler) operatorDelete(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	req := &pb.EmployeeDeleteReq{}
	if errL = e.ParseBody(c, req); errL != nil {
		return errL
	}
	req.StoreId, errL = parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	errL = service.GetEmployeeService().OperatorDelete(ctx, token.BrandId, req, token.EmpId)
	return e.SimpleRet(c, nil, errL)
}

func (e *empHandler) resetSupAdmin(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.PermissionCheck(c, "super_admin")
	if errL != nil {
		return errL
	}
	req := &pb.EmployeeSupAdminReq{}
	if errL = e.ParseBody(c, req); errL != nil {
		return errL
	}
	req.StoreId, errL = parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	// 权限判断
	hasPerm := false
	if req.StoreId == 0 {
		// 品牌超级管理员转让角色：品牌超管自己
		hasPerm, errL = service.GetRolePermissionSrv().PermissionVerify(ctx, token.BrandId, 0, token.EmpId, "super_admin")
		if errL != nil {
			return errL
		}
	} else {
		// 门店超级管理员转让角色：品牌超管，品牌管理员, 门店超管
		// 品牌级
		hasPerm, errL = service.GetRolePermissionSrv().PermissionVerify(ctx, token.BrandId, 0, token.EmpId, "admin")
		if errL != nil {
			return errL
		}
		// 门店级
		if !hasPerm {
			hasPerm, errL = service.GetRolePermissionSrv().PermissionVerify(ctx, token.BrandId, req.StoreId, token.EmpId, "super_admin")
			if errL != nil {
				return errL
			}
		}
	}
	if !hasPerm {
		return lueerror.Forbidden(fmt.Errorf("角色不满足"))
	}
	errL = service.GetEmployeeService().ResetSupAdmin(ctx, token.BrandId, req, token.EmpId)
	return e.SimpleRet(c, nil, errL)
}

func (e *empHandler) getSupAdmin(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.PermissionCheck(c, "admin")
	if errL != nil {
		return errL
	}
	storeId, errL := parseStoreId(c, false)
	if errL != nil {
		return errL
	}

	ret, errL := service.GetEmployeeService().GetSupAdmin(ctx, token.BrandId, storeId)
	return e.SimpleRet(c, ret, errL)
}

// 账号的管理品牌
func (e *empHandler) getRelatedBrands(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.VerifyAccountToken(c)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetEmployeeService().GetEmpRelatedBrands(ctx, token.GID)
	return e.SimpleRet(c, ret, errL)
}

func (e *empHandler) getOperatorInfo(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := e.VerifyBilinToken(c)
	if errL != nil {
		return errL
	}
	storeId, errL := parseStoreId(c, false)
	if errL != nil {
		return errL
	}
	ret, errL := service.GetEmployeeService().EmpOperatorInfo(ctx, token.BrandId, storeId, token.EmpId)
	return e.SimpleRet(c, ret, errL)
}
